package com.xinruke.hostels.dailymanage.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xinruke.hostels.common.cache.ManageUserCache;
import com.xinruke.hostels.common.cache.util.SysSqlParamUtil;
import com.xinruke.hostels.common.constants.SysDicConstant;
import com.xinruke.hostels.common.mybatis.MyExample;
import com.xinruke.hostels.common.po.YwRuzhuDingdan;
import com.xinruke.hostels.common.po.YwRuzhuDingdanZixiang;
import com.xinruke.hostels.common.po.YwYudingDingdanZixiang;
import com.xinruke.hostels.common.util.CustomSecurity;
import com.xinruke.hostels.common.util.DateCalendarUtil;
import com.xinruke.hostels.common.vo.query.EConditionOperator;
import com.xinruke.hostels.common.vo.query.QueryConditions;
import com.xinruke.hostels.common.vo.query.RowsDataVO;
import com.xinruke.hostels.dailymanage.interfaces.dto.inhotel.*;
import com.xinruke.hostels.dailymanage.interfaces.vo.PersonalInHotelRoomVO;
import com.xinruke.hostels.dailymanage.interfaces.vo.RoomInfoVO;
import com.xinruke.hostels.dailymanage.mapper.YwRuzhuDingdanMapper;
import com.xinruke.hostels.dailymanage.mapper.YwRuzhuDingdanZixiangMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName InHotelService
 * @CreateDate 2019/7/11
 * @Author FengXinQiang
 * @Version V1.0.0.0
 * @Decription 入住Service
 */
@Service
public class InHotelService {
    @Autowired
    YwRuzhuDingdanMapper ruzhuDingdanMapper;

    @Autowired
    YwRuzhuDingdanZixiangMapper ruzhuDingdanZixiangMapper;

    @Autowired
    ManageUserCache manageUserCache;

    /**
     * 获取入住集合
     *
     * @param inHotelQueryDTO
     * @return
     */
    public RowsDataVO<InHotelQueryResultDTO> getInHotelList(InHotelQueryDTO inHotelQueryDTO) {
        RowsDataVO<InHotelQueryResultDTO> rowsDataVO = new RowsDataVO<>();

        QueryConditions dingdanQueryConditions = inHotelQueryDTO.getQueryConditions();
        dingdanQueryConditions.addCondition("jiudianId", EConditionOperator.EQULS, manageUserCache.getJiudianId());
        dingdanQueryConditions.addCondition("shanchubiaoshi", EConditionOperator.EQULS, SysDicConstant.DELFLAG_0);

        if (!StringUtils.isEmpty(inHotelQueryDTO.getFangxingId()) || !StringUtils.isEmpty(inHotelQueryDTO.getFangjianhao())) {
            QueryConditions zixiangQueryConditions = new QueryConditions();
            zixiangQueryConditions.addCondition("fangxingId", EConditionOperator.EQULS, inHotelQueryDTO.getFangxingId());
            zixiangQueryConditions.addCondition("fangjianhao", EConditionOperator.EQULS, inHotelQueryDTO.getFangjianhao());
            zixiangQueryConditions.addCondition("shanchubiaoshi", EConditionOperator.EQULS, SysDicConstant.DELFLAG_0);
            MyExample zixiangMyExample = new MyExample(YwRuzhuDingdanZixiang.class, zixiangQueryConditions);
            List<YwRuzhuDingdanZixiang> zixiangList = ruzhuDingdanZixiangMapper.selectByExample(zixiangMyExample);
            if (zixiangList == null || zixiangList.size() == 0){
                return rowsDataVO;
            }
            List<Object> dingdanIdList = new ArrayList<>();
            for (YwRuzhuDingdanZixiang ruzhuDingdanZixiang : zixiangList) {
                dingdanIdList.add(ruzhuDingdanZixiang.getDingdanId());
            }
            dingdanQueryConditions.addCondition("id", dingdanIdList);
        }

        MyExample dingdanMyExample = new MyExample(YwRuzhuDingdan.class, dingdanQueryConditions, inHotelQueryDTO.getOrderFields());

        Page<YwRuzhuDingdan> page = PageHelper.startPage(inHotelQueryDTO.getPageNo(), inHotelQueryDTO.getPageSize());
        List<YwRuzhuDingdan> resultList = ruzhuDingdanMapper.selectByExample(dingdanMyExample);

        List<InHotelQueryResultDTO> dataList = resultList.stream().map(m -> {
            InHotelQueryResultDTO inHotelQueryResultDTO = new InHotelQueryResultDTO();
            BeanUtils.copyProperties(m, inHotelQueryResultDTO);
            inHotelQueryResultDTO.setZhengjianhao(CustomSecurity.decodeIDCard(inHotelQueryResultDTO.getZhengjianhao()));
            inHotelQueryResultDTO.setYudingrendianhua(CustomSecurity.decodeMobile(inHotelQueryResultDTO.getYudingrendianhua()));
            return inHotelQueryResultDTO;
        }).collect(Collectors.toList());

        List<RoomInfoVO> roomVOList;
        for (InHotelQueryResultDTO inHotelQueryResultDTO : dataList) {
            QueryConditions dingdanZixiangQueryConditions = new QueryConditions();
            dingdanZixiangQueryConditions.addCondition("dingdanId", EConditionOperator.EQULS, inHotelQueryResultDTO.getId());
            dingdanZixiangQueryConditions.addCondition("shanchubiaoshi", EConditionOperator.EQULS, SysDicConstant.DELFLAG_0);

            MyExample dingdanZixiangMyExample = new MyExample(YwRuzhuDingdanZixiang.class, dingdanZixiangQueryConditions);
            List<YwRuzhuDingdanZixiang> dingdanZixiangList = ruzhuDingdanZixiangMapper.selectByExample(dingdanZixiangMyExample);

            String fangjianInfo = "";
            roomVOList = new ArrayList<>();
            for (YwRuzhuDingdanZixiang ruzhuDingdanZixiang : dingdanZixiangList) {
                if (!StringUtils.isEmpty(fangjianInfo)) {
                    fangjianInfo = fangjianInfo + "| ";
                }
                fangjianInfo = fangjianInfo + ruzhuDingdanZixiang.getFangxingmingcheng() + "（" + ruzhuDingdanZixiang.getFangjianhao() + "）";

                RoomInfoVO roomInfoVO = new RoomInfoVO();
                roomInfoVO.setFangxingId(ruzhuDingdanZixiang.getFangxingId());
                roomInfoVO.setFangxingmingcheng(ruzhuDingdanZixiang.getFangxingmingcheng());
                roomInfoVO.setFangjianId(ruzhuDingdanZixiang.getFangjianId());
                roomInfoVO.setFangjianhao(ruzhuDingdanZixiang.getFangjianhao());

                PersonalInHotelRoomVO personalInHotelRoomVO = new PersonalInHotelRoomVO();
                BeanUtils.copyProperties(ruzhuDingdanZixiang, personalInHotelRoomVO);
                roomVOList.add(roomInfoVO);
            }
            inHotelQueryResultDTO.setRoomVOList(roomVOList);
            inHotelQueryResultDTO.setFangjianInfo(fangjianInfo);
        }

        rowsDataVO.setPageNo(page.getPageNum());
        rowsDataVO.setTotalRecords(page.getTotal());
        rowsDataVO.setTotalPages(page.getPages());
        rowsDataVO.setRows(dataList);

        return rowsDataVO;
    }

    /**
     * 根据酒店ID获取入住集合
     *
     * @param inHotelQueryDTO
     * @return
     */
    public RowsDataVO<InHotelQueryResultDTO> getInHotelListByHotelId(InHotelQueryByHotelIdDTO inHotelQueryDTO) {
        RowsDataVO<InHotelQueryResultDTO> rowsDataVO = new RowsDataVO<>();

        QueryConditions dingdanQueryConditions = inHotelQueryDTO.getQueryConditions();
        dingdanQueryConditions.addCondition("jiudianId", EConditionOperator.EQULS, inHotelQueryDTO.getJiudianId());
        dingdanQueryConditions.addCondition("shanchubiaoshi", EConditionOperator.EQULS, SysDicConstant.DELFLAG_0);

        if (!StringUtils.isEmpty(inHotelQueryDTO.getFangxingId()) || !StringUtils.isEmpty(inHotelQueryDTO.getFangjianhao())) {
            QueryConditions zixiangQueryConditions = new QueryConditions();
            zixiangQueryConditions.addCondition("fangxingId", EConditionOperator.EQULS, inHotelQueryDTO.getFangxingId());
            zixiangQueryConditions.addCondition("fangjianhao", EConditionOperator.EQULS, inHotelQueryDTO.getFangjianhao());
            zixiangQueryConditions.addCondition("shanchubiaoshi", EConditionOperator.EQULS, SysDicConstant.DELFLAG_0);
            MyExample zixiangMyExample = new MyExample(YwRuzhuDingdanZixiang.class, zixiangQueryConditions);
            List<YwRuzhuDingdanZixiang> zixiangList = ruzhuDingdanZixiangMapper.selectByExample(zixiangMyExample);
            if (null == zixiangList || zixiangList.size() == 0) {
                return new RowsDataVO<>();
            }
            List<Object> dingdanIdList = new ArrayList<>();
            for (YwRuzhuDingdanZixiang ruzhuDingdanZixiang : zixiangList) {
                dingdanIdList.add(ruzhuDingdanZixiang.getDingdanId());
            }
            dingdanQueryConditions.addCondition("id", dingdanIdList);
        }

        MyExample dingdanMyExample = new MyExample(YwRuzhuDingdan.class, dingdanQueryConditions, inHotelQueryDTO.getOrderFields());

        Page<YwRuzhuDingdan> page = PageHelper.startPage(inHotelQueryDTO.getPageNo(), inHotelQueryDTO.getPageSize());
        List<YwRuzhuDingdan> resultList = ruzhuDingdanMapper.selectByExample(dingdanMyExample);

        List<InHotelQueryResultDTO> dataList = resultList.stream().map(m -> {
            InHotelQueryResultDTO inHotelQueryResultDTO = new InHotelQueryResultDTO();
            String jiudianMingcheng = SysSqlParamUtil.getSysParamDesc("JIUDIAN.MINGCHENG", m.getJiudianId());
            inHotelQueryResultDTO.setJiudianMingcheng(jiudianMingcheng);
            BeanUtils.copyProperties(m, inHotelQueryResultDTO);
            inHotelQueryResultDTO.setZhengjianhao(CustomSecurity.decodeIDCard(inHotelQueryResultDTO.getZhengjianhao()));
            inHotelQueryResultDTO.setYudingrendianhua(CustomSecurity.decodeMobile(inHotelQueryResultDTO.getYudingrendianhua()));
            return inHotelQueryResultDTO;
        }).collect(Collectors.toList());

        List<RoomInfoVO> roomVOList;
        for (InHotelQueryResultDTO inHotelQueryResultDTO : dataList) {
            QueryConditions dingdanZixiangQueryConditions = new QueryConditions();
            dingdanZixiangQueryConditions.addCondition("dingdanId", EConditionOperator.EQULS, inHotelQueryResultDTO.getId());
            dingdanZixiangQueryConditions.addCondition("shanchubiaoshi", EConditionOperator.EQULS, SysDicConstant.DELFLAG_0);

            MyExample dingdanZixiangMyExample = new MyExample(YwRuzhuDingdanZixiang.class, dingdanZixiangQueryConditions);
            List<YwRuzhuDingdanZixiang> dingdanZixiangList = ruzhuDingdanZixiangMapper.selectByExample(dingdanZixiangMyExample);

            String fangjianInfo = "";
            roomVOList = new ArrayList<>();
            for (YwRuzhuDingdanZixiang ruzhuDingdanZixiang : dingdanZixiangList) {
                if (!StringUtils.isEmpty(fangjianInfo)) {
                    fangjianInfo = fangjianInfo + "| ";
                }
                fangjianInfo = fangjianInfo + ruzhuDingdanZixiang.getFangxingmingcheng() + "（" + ruzhuDingdanZixiang.getFangjianhao() + "）";

                RoomInfoVO roomInfoVO = new RoomInfoVO();
                roomInfoVO.setFangxingId(ruzhuDingdanZixiang.getFangxingId());
                roomInfoVO.setFangxingmingcheng(ruzhuDingdanZixiang.getFangxingmingcheng());
                roomInfoVO.setFangjianId(ruzhuDingdanZixiang.getFangjianId());
                roomInfoVO.setFangjianhao(ruzhuDingdanZixiang.getFangjianhao());

                PersonalInHotelRoomVO personalInHotelRoomVO = new PersonalInHotelRoomVO();
                BeanUtils.copyProperties(ruzhuDingdanZixiang, personalInHotelRoomVO);
                roomVOList.add(roomInfoVO);
            }
            inHotelQueryResultDTO.setRoomVOList(roomVOList);
            inHotelQueryResultDTO.setFangjianInfo(fangjianInfo);
        }

        rowsDataVO.setPageNo(page.getPageNum());
        rowsDataVO.setTotalRecords(page.getTotal());
        rowsDataVO.setTotalPages(page.getPages());
        rowsDataVO.setRows(dataList);

        return rowsDataVO;
    }

    /**
     * 获取入住详情
     *
     * @param inHotelDetailQueryDTO
     * @return
     */
    public InHotelDetailQueryResultDTO getInHotelDetail(InHotelDetailQueryDTO inHotelDetailQueryDTO) {
        InHotelDetailQueryResultDTO inHotelDetailQueryResultDTO = new InHotelDetailQueryResultDTO();

        int ruzhuId = inHotelDetailQueryDTO.getRuzhuId();

        YwRuzhuDingdan ruzhuDingdan = ruzhuDingdanMapper.selectByPrimaryKey(ruzhuId);
        if (ruzhuDingdan == null) {
            return inHotelDetailQueryResultDTO;
        }
        BeanUtils.copyProperties(ruzhuDingdan, inHotelDetailQueryResultDTO);
        int ruzhutianshu = DateCalendarUtil.getNaturalDayDifferent(inHotelDetailQueryResultDTO.getRuzhushijian(), inHotelDetailQueryResultDTO.getLikaishijian());
        inHotelDetailQueryResultDTO.setRuzhutianshu(ruzhutianshu);
        inHotelDetailQueryResultDTO.setZhengjianhao(CustomSecurity.decodeIDCard(inHotelDetailQueryResultDTO.getZhengjianhao()));
        inHotelDetailQueryResultDTO.setYudingrendianhua(CustomSecurity.decodeMobile(inHotelDetailQueryResultDTO.getYudingrendianhua()));

        QueryConditions fangjianQueryCondition = new QueryConditions();
        fangjianQueryCondition.addCondition("dingdanId", EConditionOperator.EQULS, ruzhuId);
        fangjianQueryCondition.addCondition("shanchubiaoshi", EConditionOperator.EQULS, SysDicConstant.DELFLAG_0);
        MyExample fangjianMyExample = new MyExample(YwYudingDingdanZixiang.class, fangjianQueryCondition);
        List<YwRuzhuDingdanZixiang> fangjianResultList = ruzhuDingdanZixiangMapper.selectByExample(fangjianMyExample);

        List<PersonalInHotelRoomVO> roomVOList = fangjianResultList.stream().map(m -> {
            PersonalInHotelRoomVO personalInHotelRoomVO = new PersonalInHotelRoomVO();
            BeanUtils.copyProperties(m, personalInHotelRoomVO);
            return personalInHotelRoomVO;
        }).collect(Collectors.toList());

        inHotelDetailQueryResultDTO.setRoomVOList(roomVOList);

        return inHotelDetailQueryResultDTO;
    }

}
